from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy as _
from import_export.admin import ExportActionMixin

from app.models import *

admin.site.site_header = '后台管理系统'
admin.site.site_title = 'Jiaozi'
admin.site.index_title = '后台管理系统'


class SimpleModelAdmin(ExportActionMixin, admin.ModelAdmin):
    view_on_site = False


class AutoUpdateUserModelAdmin(ExportActionMixin, admin.ModelAdmin):
    view_on_site = False
    readonly_fields = ('created_by', 'updated_by')
    date_hierarchy = 'datetime_created'

    def save_model(self, request, instance, form, change):
        user = request.user
        instance = form.save(commit=False)
        if not change or not instance.created_by:
            instance.created_by = user
        instance.updated_by = user
        instance.save()
        form.save_m2m()


@admin.register(WxUser)
class WxUserAdmin(UserAdmin):
    readonly_fields = (
        'last_login', 'date_joined', 'nick_name', 'city', 'province', 'country', 'avatar_url', 'default_user_address'
    )
    list_display = [
        'pk', 'username', 'nick_name', 'mobile',
        'is_staff', 'is_superuser', 'date_joined', 'last_login']
    list_display_links = ['pk', 'username', 'nick_name']
    search_fields = [
        'username', 'openid', 'email', 'mobile', 'first_name', 'last_name', 'nick_name']
    autocomplete_fields = ['vip_level']
    list_filter = ('is_staff', 'is_superuser', 'groups')
    fieldsets = (
        (_('基础信息'), {'fields': ('username', 'password', 'openid')}),
        (_('个人信息'), {'fields': (
            'nick_name', 'first_name', 'last_name', 'avatar_url', 'gender', 'date_of_birth', 'desc'
        )}),
        (_('联络信息'), {'fields': ('mobile', 'email',)}),
        (_('地址信息'), {'fields': ('city', 'province', 'country')}),
        (_('分类信息'), {'fields': ('vip_level',)}),
        (_('权限管理'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups')}),
        (_('登录信息'), {'fields': ('last_login', 'date_joined')}),
    )


@admin.register(UserPoint)
class UserPointAdmin(SimpleModelAdmin):
    readonly_fields = ['user']
    list_display = ['pk', 'user', 'amount', 'amount_total', 'amount_current', 'notes']
    list_display_links = ['pk', 'user']
    search_fields = ['user__mobile', 'user__nick_name', 'notes']


@admin.register(UserAddress)
class UserAddressAdmin(SimpleModelAdmin):
    readonly_fields = ['user']
    list_display = ['pk', 'user', 'name', 'mobile', 'address', 'area', 'default']
    list_display_links = ['pk', 'user']
    list_filter = ['default']
    search_fields = ['user__id', 'user__mobile', 'name', 'mobile', 'address', 'area']


@admin.register(FeedBack)
class FeedBackAdmin(SimpleModelAdmin):
    readonly_fields = ['user']
    list_display = ['pk', 'user', 'body', 'is_checked']
    list_display_links = ['pk', 'user']
    search_fields = ['body']


@admin.register(WxHome)
class WxHomeAdmin(SimpleModelAdmin):
    list_display = [
        'pk', 'logo_circular', 'logo_square', 'image_1', 'image_2', 'image_3', 'image_4', 'image_5',
        'vip_background', 'is_active']
    list_display_links = ['pk']
    list_filter = ['is_active']


@admin.register(SmsRecord)
class SmsRecordAdmin(SimpleModelAdmin):
    list_display = ['pk', 'mobile', 'code', 'is_active', 'datetime_created', 'datetime_updated']
    list_display_links = ['pk', 'mobile']
    search_fields = ['mobile']
    list_filter = ['is_active']


@admin.register(Document)
class DocumentAdmin(SimpleModelAdmin):
    list_display = ['pk', 'title', 'key_word', 'is_active']
    list_display_links = ['pk', 'title']
    search_fields = ['title', 'key_word']
    list_filter = ['is_active']
